package com.qymerge.manage.picture.service;

import com.qymerge.manage.core.utils.Env;
import com.qymerge.manage.core.utils.GsonUtil;
import com.qymerge.manage.core.utils.HttpRequestUtil;
import com.qymerge.manage.picture.dto.BatchUploadImgItemDTO;
import com.qymerge.manage.picture.dto.BatchUploadImgResultDTO;
import com.qymerge.manage.picture.dto.BatchUploadRemoteImgDTO;
import com.qymerge.manage.picture.dto.PictureDTO;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

/**
 * Desc
 * User cong.li
 * Date 2017/5/30
 * Time 17:40
 */
@Service
public class PictureService {

    public PictureDTO upload(File file, int typeId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("img", file);

        String url = String.format("%s/picture/upload/%s", Env.apiDomain, typeId);
        String result = HttpRequestUtil.requestPost(url, null, map);
        return GsonUtil.json2T(result, PictureDTO.class);
    }

    /**
     * 上传图片
     *
     * @param file
     * @param typeId
     * @return
     */
    public Map uploadImg(File file, int typeId) {
        Map resultMap = new HashMap();
        resultMap.put("state", "FAIL");
        if (file == null) {
            return resultMap;
        }
        PictureDTO responseDTO = upload(file, typeId);
        if (responseDTO.getCode() == 1) {
            if (responseDTO.getData() != null) {
                String img = responseDTO.getData().getPath();
                String imgName = img.substring(img.lastIndexOf("/") + 1, img.length());
                resultMap.clear();
                resultMap.put("state", "SUCCESS");
                resultMap.put("url", img);
                resultMap.put("title", imgName);
                resultMap.put("original", imgName);
            }
            file.delete();
        }
        return resultMap;
    }

    /**
     * 批量上传图片
     *
     * @param imgs   远程图片地址集合
     * @param typeId
     * @return
     */
    public List<BatchUploadImgItemDTO> batchUploadRemoteImg(Set<String> imgs, int typeId) {
        List<BatchUploadImgItemDTO> list = new LinkedList<BatchUploadImgItemDTO>();
        String url = String.format("%s/picture/batchUploadRemoteImg/%s", Env.apiDomain, typeId);

        Map paramsMap = new HashMap();
        paramsMap.put("imgs", imgs);
        String result = HttpRequestUtil.requestPostBody(url, GsonUtil.map2Json(paramsMap));
        BatchUploadRemoteImgDTO responseDTO = GsonUtil.json2T(result, BatchUploadRemoteImgDTO.class);
        if (responseDTO.getCode() == 1) {
            if (responseDTO.getData() != null) {
                BatchUploadImgResultDTO resultDTO = responseDTO.getData();
                list = resultDTO.getResult();
            }
        }
        return list;
    }

    /**
     * 批量上传图片
     *
     * @param img   远程图片地址
     * @param typeId
     * @return
     */
    public List<BatchUploadImgItemDTO> batchUploadRemoteImg(String img, int typeId) {
        List<BatchUploadImgItemDTO> list = new LinkedList<BatchUploadImgItemDTO>();
        String url = String.format("%s/picture/batchUploadRemoteImg/%s", Env.apiDomain, typeId);

        Map paramsMap = new HashMap();
        paramsMap.put("imgs", Arrays.asList(img));
        String result = HttpRequestUtil.requestPostBody(url, GsonUtil.map2Json(paramsMap));
        BatchUploadRemoteImgDTO responseDTO = GsonUtil.json2T(result, BatchUploadRemoteImgDTO.class);
        if (responseDTO.getCode() == 1) {
            if (responseDTO.getData() != null) {
                BatchUploadImgResultDTO resultDTO = responseDTO.getData();
                list = resultDTO.getResult();
            }
        }
        return list;
    }


}
